package com.shujia.scala

import java.sql.{Connection, DriverManager, PreparedStatement, ResultSet}

object Demo7JDBC {
  def main(args: Array[String]): Unit = {
    //1、加载驱动
    Class.forName("com.mysql.jdbc.Driver")
    //2、创建数据库连接
    val con: Connection = DriverManager.getConnection("jdbc:mysql://master:3306/bigdata31", "root", "123456")
    //3、编写sql查询数据
    val stat: PreparedStatement = con.prepareStatement("select * from students")
    //4、执行查询
    val resultSet: ResultSet = stat.executeQuery()
    //5、循环解析数据
    while (resultSet.next()) {
      val id: Long = resultSet.getLong("id")
      val name: String = resultSet.getString("name")
      val age: Long = resultSet.getLong("age")
      val gender: String = resultSet.getString("gender")
      val clazz: String = resultSet.getString("clazz")
      println(s"$id\t$name\t$age\t$gender\t$clazz")
    }
    //6、关闭连接
    con.close()
  }
}
